/*
 * Modified sample code from mgwt showcase
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.dhbw.vs.permachat.client.activities.chat;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.event.dom.client.HasKeyUpHandlers;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.dialog.PopinDialog;
import com.googlecode.mgwt.ui.client.widget.Button;
import com.googlecode.mgwt.ui.client.widget.HeaderButton;
import com.googlecode.mgwt.ui.client.widget.HeaderPanel;
import com.googlecode.mgwt.ui.client.widget.LayoutPanel;
import com.googlecode.mgwt.ui.client.widget.MTextBox;
import com.googlecode.mgwt.ui.client.widget.ProgressIndicator;
import com.googlecode.mgwt.ui.client.widget.ScrollPanel;
import com.googlecode.mgwt.ui.client.widget.celllist.BasicCell;
import com.googlecode.mgwt.ui.client.widget.celllist.CellListWithHeader;

import de.dhbw.vs.permachat.client.dto.ChatMessage;

/**
 * @author flo
 * 
 */
public class ChatViewGwtImpl implements ChatView {
	private static int MAX_MESSAGE_COUNT = 50;

	private CellListWithHeader<ChatMessage> list;
	private LayoutPanel main;
	private HeaderPanel headerPanel;
	private HeaderButton headerBackButton;
	private PopinDialog progressDialog;
	private List<ChatMessage> chatMessageList;
	private ScrollPanel scrollPanel;
	private MTextBox inputBox;
	private Button sendButton;
	//private Anchor dummyHiddenFocusableElement;

	public ChatViewGwtImpl() {
		chatMessageList = new LinkedList<ChatMessage>();
		
		main = new LayoutPanel();

		headerPanel = new HeaderPanel();

		headerBackButton = new HeaderButton();

		headerPanel.setLeftWidget(headerBackButton);
		headerBackButton.setBackButton(true);
		headerBackButton.setVisible(!MGWT.getOsDetection().isAndroid());

		main.add(headerPanel);

		scrollPanel = new ScrollPanel();

		list = new CellListWithHeader<ChatMessage>(new BasicCell<ChatMessage>() {

			@Override
			public String getDisplayString(ChatMessage model) {
				return model.getChatUser() + ": " + model.getTextMessage();
			}
		});

		list.getCellList().setRound(true);
		list.getCellList().addStyleName("noBackgroundImage");

		scrollPanel.setWidget(list);
		scrollPanel.setScrollingEnabledX(false);
		//scrollPanel.setUsePos(true);

		main.add(scrollPanel);
		
		//LayoutPanel horizontalLayoutPanel = new LayoutPanel();
		HorizontalPanel horizontalLayoutPanel = new HorizontalPanel();
		horizontalLayoutPanel.addStyleName("paddedLayoutPanel");
		horizontalLayoutPanel.setWidth("100%");
		//horizontalLayoutPanel.setHorizontal(true);
		//horizontalLayoutPanel.setHeight("3em");
		
		inputBox = new MTextBox();
		inputBox.getElement().getFirstChildElement().addClassName("myRoundCorners");
		inputBox.setPlaceHolder("New Message");
		inputBox.setWidth("100%");
		inputBox.setHeight("3em");
		
		horizontalLayoutPanel.add(inputBox);
		horizontalLayoutPanel.setCellVerticalAlignment(inputBox, HorizontalPanel.ALIGN_MIDDLE);
		
		sendButton = new Button("Go...");
		sendButton.setConfirm(true);
		sendButton.setWidth("3em");
		horizontalLayoutPanel.add(sendButton);
		horizontalLayoutPanel.setCellWidth(sendButton, "3em");
		horizontalLayoutPanel.setCellVerticalAlignment(sendButton, HorizontalPanel.ALIGN_MIDDLE);
		
		main.add(horizontalLayoutPanel);
		
		//hack for hiding mobile keyboard after sending message
		//https://groups.google.com/forum/#!topic/mgwt/3EIFyHEQHnI
		/*OnlyToBeUsedInGeneratedCodeStringBlessedAsSafeHtml sh = new OnlyToBeUsedInGeneratedCodeStringBlessedAsSafeHtml("<span></span>");
		dummyHiddenFocusableElement = new Anchor(sh);*/
	}

	@Override
	public Widget asWidget() {
		return main;
	}

	@Override
	public void setTitle(String text) {
		headerPanel.setCenter(text);

	}

	@Override
	public HasTapHandlers getBackButton() {
		return headerBackButton;
	}

	@Override
	public void setLeftButtonText(String text) {
		headerBackButton.setText(text);

	}

	@Override
	public HasText getFirstHeader() {
		return list.getHeader();
	}

	@Override
	public void showProgressIndicator() {
		this.progressDialog = new PopinDialog();
		this.progressDialog.add(new ProgressIndicator());
		this.progressDialog.setCenterContent(true);
		this.progressDialog.setHideOnBackgroundClick(false);
		this.progressDialog.setPanelToOverlay(this.main);
		this.progressDialog.setShadow(true);
		
		this.progressDialog.show();
	}

	@Override
	public void hideProgressIndicator() {
		if(this.progressDialog != null){
			this.progressDialog.hide();
			this.progressDialog = null;
		}
	}

	@Override
	public void addChatMessages(List<ChatMessage> newChatMessages) {
		if(newChatMessages != null && !newChatMessages.isEmpty()){
			this.chatMessageList.addAll(newChatMessages);
			
			int chatMessageListSize = this.chatMessageList.size();
			if(chatMessageListSize > MAX_MESSAGE_COUNT){
				this.chatMessageList.subList(0, chatMessageListSize - MAX_MESSAGE_COUNT).clear();
			}
			
			this.list.getCellList().render(this.chatMessageList);
			this.scrollPanel.refresh();
			this.scrollPanel.scrollTo(0, this.scrollPanel.getMaxScrollY());
		}
	}

	@Override
	public void reset() {
		this.chatMessageList.clear();
		this.list.getCellList().render(this.chatMessageList);
		this.hideProgressIndicator();
	}

	@Override
	public HasTapHandlers getSendButton() {
		return this.sendButton;
	}

	@Override
	public HasText getInputBox() {
		return this.inputBox;
	}

	@Override
	public HasKeyUpHandlers getInputBoxSender() {
		return this.inputBox;
	}

	/*
	private void blurInputBox() {
		//this.inputBox.getElement().blur();
		//this.dummyHiddenFocusableElement.setFocus(true);
		MGWT.hideKeyBoard();
	}*/

	@Override
	public void sendSucceeded() {
		if(MGWT.getOsDetection().isDesktop()){
			this.inputBox.setFocus(true);
			this.inputBox.getElement().focus();
		} else{
			//if soft keyboard does not collapse automatically
			//call blurInputBox()
		}
	}

}
